var allTimeoutList = [];

var now = getNowDate();

var done = {
	color : "#858585",
	bg: "#f3f2f2",
	borderLeft: "2px solid #858585"
};

var khqyhsBg = $(".khqyhs").css("background-color");
var khqyhs = {
		color : khqyhsBg,
		bg: buildRgba(khqyhsBg, 0.2),
		borderLeft: "2px solid " + khqyhsBg
	};

var xxplTzzbgBg = $(".xxpl-tzzbg").css("background-color");
var xxplTzzbg = {
		color : xxplTzzbgBg,
		bg: buildRgba(xxplTzzbgBg, 0.2),
		borderLeft: "2px solid " + xxplTzzbgBg
	};

var xxplJgbgBg = $(".xxpl-jgbg").css("background-color");
var xxplJgbg = {
		color : xxplJgbgBg,
		bg: "#e8e5ff",
		borderLeft: "2px solid " + xxplJgbgBg
	};

function showCalendar() {
	
	allTimeoutList = [];
	now = getNowDate();
	
	var khqyhsDoneEvents = {
			events: [],
			color: done.bg,
			textColor: done.color,
			borderLeft: done.borderLeft,
			className: "done"
		};
	var khqyhsDoingEvents = {
			events: [],
			color: khqyhs.bg,
			textColor: khqyhs.color,
			borderLeft: khqyhs.borderLeft,
			className: "doing"
		};
	var khqyhsNodoEvents = {
			events: [],
			color: khqyhs.bg,
			textColor: khqyhs.color,
			borderLeft: khqyhs.borderLeft,
			className: ""
		};
	var khqyhsTimeoutEvents = {
			events: [],
			color: khqyhs.bg,
			textColor: khqyhs.color,
			borderLeft: khqyhs.borderLeft,
			className: "timeout"
		};
	
	var xxplTzzbgDoneEvents = {
			events: [],
			color: done.bg,
			textColor: done.color,
			borderLeft: done.borderLeft,
			className: "done"
		};
	var xxplTzzbgDoingEvents = {
			events: [],
			color: xxplTzzbg.bg,
			textColor: xxplTzzbg.color,
			borderLeft: xxplTzzbg.borderLeft,
			className: "doing"
		};
	var xxplTzzbgNodoEvents = {
			events: [],
			color: xxplTzzbg.bg,
			textColor: xxplTzzbg.color,
			borderLeft: xxplTzzbg.borderLeft,
			className: ""
		};
	var xxplTzzbgTimeoutEvents = {
			events: [],
			color: xxplTzzbg.bg,
			textColor: xxplTzzbg.color,
			borderLeft: xxplTzzbg.borderLeft,
			className: "timeout"
		};
	
	var xxplJgbgDoneEvents = {
			events: [],
			color: done.bg,
			textColor: done.color,
			borderLeft: done.borderLeft,
			className: "done"
		};
	var xxplJgbgDoingEvents = {
			events: [],
			color: xxplJgbg.bg,
			textColor: xxplJgbg.color,
			borderLeft: xxplJgbg.borderLeft,
			className: "doing"
		};
	var xxplJgbgNodoEvents = {
			events: [],
			color: xxplJgbg.bg,
			textColor: xxplJgbg.color,
			borderLeft: xxplJgbg.borderLeft,
			className: ""
		};
	var xxplJgbgTimeoutEvents = {
			events: [],
			color: xxplJgbg.bg,
			textColor: xxplJgbg.color,
			borderLeft: xxplJgbg.borderLeft,
			className: "timeout"
		};
	
	
	$.post(PATH_PREFIX + "listAllTodo.do", {}, function(data) {
		for(var i = 0; i < data.length; i++) {
			var o = data[i];
			var id = o.id;
			var todoDate = o.todoDate;
			var serviceType = o.serviceType;
			var status = o.status;
			
			if(serviceType == "CUST_EQUITY") {
				joinEvents(khqyhsDoneEvents, khqyhsTimeoutEvents, khqyhsDoingEvents, khqyhsNodoEvents, o);
			} else if(serviceType == "INFO_DIS_INVESTER") {
				joinEvents(xxplTzzbgDoneEvents, xxplTzzbgTimeoutEvents, xxplTzzbgDoingEvents, xxplTzzbgNodoEvents, o);
			} else if(serviceType == "INFO_DIS_REGULATORY") {
				joinEvents(xxplJgbgDoneEvents, xxplJgbgTimeoutEvents, xxplJgbgDoingEvents, xxplJgbgNodoEvents, o);
			}
		}
		
		allTimeoutList.push(khqyhsTimeoutEvents);
		allTimeoutList.push(xxplTzzbgTimeoutEvents);
		allTimeoutList.push(xxplJgbgTimeoutEvents);
		
		$('#calendar').fullCalendar({
			header : {
				left : 'prev,next today',
				center : 'title',
				// right: 'month,listWeek'
				right : 'month,agendaWeek,agendaDay,listWeek'
			},
			defaultDate : now,
			navLinks : true, // can click day/week names to navigate views
			editable : false,
			eventLimit : true, // allow "more" link when too many events
			eventLimitText : function() {
				return '更多'
			},
			eventSources : [
				khqyhsDoneEvents,
				khqyhsDoingEvents,
				khqyhsNodoEvents,
				khqyhsTimeoutEvents,
				xxplTzzbgDoneEvents,
				xxplTzzbgDoingEvents,
				xxplTzzbgNodoEvents,
				xxplTzzbgTimeoutEvents,
				xxplJgbgDoneEvents,
				xxplJgbgDoingEvents,
				xxplJgbgNodoEvents,
				xxplJgbgTimeoutEvents
			]
		});
		
		$("#timeoutList").height($("#calendar").height() - 2);
		$("#timeoutList").empty();
		
		var sortTimeoutList = [];
		
		for(var i = 0; i < allTimeoutList.length; i++) {
			var p = allTimeoutList[i];
			for(var j = 0; j < p.events.length; j++) {
				var event = p.events[j];
				
				var o = {
						id: event.id,
						title: event.title,
						start: event.start,
						bg: p.color,
						color: p.textColor,
						borderLeft: p.borderLeft
					};
				
				sortTimeoutList.push(o);
			}
		}
		
		sortTimeoutList = sortTimeoutList.sort(function(a, b) {
			if(a.start > b.start) {
				return -1;
			} 
			if(a.start == b.start) {
				return 0;
			}
			return 1;
		});
		
		var td = [];
		for(var i = 0; i < sortTimeoutList.length; i++) {
			var o = sortTimeoutList[i];
			td.push('<a class="fc-day-grid-event fc-event timeout" href="javascript:TODO_OUTPUT.fileDetail(\'' + o.id + '\');" style="background-color:' + o.bg + ';border-color:' + o.bg + ';color:' + o.color + '">');
			td.push('	<div class="fc-content" style="border-left: ' + o.borderLeft + '"> ');
			td.push('		<span class="hejm-icon"></span>');
			td.push('		<span class="fc-title" title="' + o.title + '">' + o.title + '</span>');
			td.push('		<span class="timeout-item-time">' + o.start + '</span>');
			td.push('	</div>');
			td.push('</a>');
		}
		$(".timeout-count").html(sortTimeoutList.length);
		$("#timeoutList").html(td.join(''));
	}, 'json');
}


function buildRgba(color, o) {
	if(color && color.indexOf("rgb") != -1) {
		color = color.replace("rgb", "rgba");
		return color.substr(0, color.length-1) + ', ' + o + ')';
	} else {
		return color;
	}
}

function buildTitle(o) {
	if(o.todoTypeName) {
		if(o.prjSname) {
			return o.prjSname + "-" + o.todoTypeName;
		} else {
			return o.todoTypeName;
		}
	} else {
		return "";  // 不可能
	}
}

function joinEvents(doneEvents, timeoutEvents, doingEvents, nodoEvents, o) {
	var event = {};
	event.id = o.id;
	event.start = o.todoDate;
	event.title = buildTitle(o);
	event.url = "javascript:TODO_OUTPUT.fileDetail('" + o.id + "');";
	
	if(o.status == 'E') {
		doneEvents.events.push(event);
	} else {
		if(o.todoDate < now) {  // 超时
			timeoutEvents.events.push(event);
		} else if(o.todoDate == now) {  // 进行中
			doingEvents.events.push(event);
		} else {  // 未进行
			nodoEvents.events.push(event);
		}
	}
	
}